package com.iiapk.location;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SearchHomeLocationActivtiy extends Activity implements TextWatcher {

	private EditText searchText;
	private TextView phoneNumberText, cardTypeText, homeLocationText;
	private Button cleanBtn;
	private int screenWidth;

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			String text = (String) msg.obj;
			checkNumber(text);
		};
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.location_search_layout);

		Display display = getWindowManager().getDefaultDisplay();
		screenWidth = display.getWidth();

		cleanBtn = (Button) findViewById(R.id.clean_button);
		searchText = (EditText) findViewById(R.id.location_search_text);
		searchText.getLayoutParams().width = (int) (0.80 * screenWidth);
		searchText.addTextChangedListener(this);
		phoneNumberText = (TextView) findViewById(R.id.phone_number_text);
		homeLocationText = (TextView) findViewById(R.id.home_location_text);
		cardTypeText = (TextView) findViewById(R.id.card_type_text);

		cleanBtn.setOnClickListener(new CleanBtn());
		new CallerLocationAdapter(this).open();
	}

	class CleanBtn implements OnClickListener {
		@Override
		public void onClick(View v) {
			searchText.setText("");
			resultSet();
		}

	}

	private void checkNumber(String phoneNumber) {
		if (phoneNumber != null && !phoneNumber.trim().equals("")
				&& phoneNumber.length() >= 3) {
			// remove NationCode(86 ……)、IPprefix(17951、17909……)
			String number = CallerLocation.removePrefix(SearchHomeLocationActivtiy.this, phoneNumber);
			String result = CallerLocation.getLocationInfo(SearchHomeLocationActivtiy.this, number);
			if (result != null) {
				phoneNumberText.setText(number);
				String mobile = this.getString(R.string.yidong);
				String unicom = this.getString(R.string.liantong);
				String telecom = this.getString(R.string.dianxin);
				// mobile phone
				if (result.contains(mobile) || result.contains(unicom)|| result.contains(telecom)) {
					String cardType = result.substring(result.length() - 2);
					String location = result.substring(0, result.length() - 2);
					cardTypeText.setText(cardType);
					homeLocationText.setText(location);
				}
				// fix phone
				else {
					homeLocationText.setText(result);
				}
			} else {
				resultSet();
			}
		} else {
			resultSet();
		}
	}

	private void resultSet() {
		if (!homeLocationText.getText().equals("")|| !cardTypeText.getText().equals("")) {
			phoneNumberText.setText("");
			homeLocationText.setText("");
			cardTypeText.setText("");
		}
	}

	@Override
	public void afterTextChanged(Editable s) {
		final String input = s.toString();
		final Message msg = new Message();
		msg.obj = input;
		Thread thread = new Thread() {
			public void run() {
				handler.sendMessage(msg);
			}
		};
		thread.start();
	}
	
	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,int after) {
	}
	
	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
	}
	
}
